package com.neusoft.lohas.modules.ivm.entity.purchase;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryMember;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.product.Supplier;

/**
 * 采购指令主表Entity
 * 
 * @author Jackie.Liu
 * @version 2014-12-29
 */
@Entity
@Table(name = "ivm_purchase")
@DynamicInsert
@DynamicUpdate
public class Purchase extends IdEntity<Purchase> {

	private static final long serialVersionUID = 1L;
	private Merchant merchant; // 所属商户
	private String code; // 编号
	private DeliveryMember member; //采购员
	private Supplier supplier; //采购供应商
	private String state; //处理状态：0未完成,1部分完成,2已完成
	private List<PurchaseDetail> detailList = Lists.newArrayList();  //采购指令对应的采购清单明细列表
	private List<PurchaseDetailItem> detailItemList = Lists.newArrayList(); //采购指令对应的采购清单（不合并采购数量信息）
	
	public Purchase() {
		super();
	}

	public Purchase(String id) {
		this();
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name="merchant_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull(message="归属商户不能为空")
	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	@ManyToOne
	@JoinColumn(name = "member_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull(message="所属采购员不能为空")
	public DeliveryMember getMember() {
		return member;
	}
	
	public void setMember(DeliveryMember member) {
		this.member = member;
	}
	
	@ManyToOne
	@JoinColumn(name="supplier_id")
	@NotFound(action = NotFoundAction.IGNORE)
	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "purchase")
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("update_date")
	@NotFound(action = NotFoundAction.IGNORE)
	public List<PurchaseDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<PurchaseDetail> detailList) {
		this.detailList = detailList;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "purchase")
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@NotFound(action = NotFoundAction.IGNORE)
	public List<PurchaseDetailItem> getDetailItemList() {
		return detailItemList;
	}

	public void setDetailItemList(List<PurchaseDetailItem> detailItemList) {
		this.detailItemList = detailItemList;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	
	@Transient
	public int getPurchaseFinishedNum(){
		int num = 0;
		if (this.getDetailList().size()>0) {
			for (PurchaseDetail detail : this.getDetailList()) {
				if (detail.getState().equals("1")) {
					num++;
				}
			}
		}
		return num;
	}
	
}
